<template>
  <div class="login-bg">
    <svg class="tech-bg" viewBox="0 0 1920 1080" fill="none" xmlns="http://www.w3.org/2000/svg">
      <g opacity="0.15">
        <circle cx="400" cy="200" r="120" stroke="#4fd1c5" stroke-width="2" />
        <circle cx="1600" cy="900" r="180" stroke="#3182ce" stroke-width="2" />
        <line x1="400" y1="200" x2="1600" y2="900" stroke="#63b3ed" stroke-width="1.5" />
        <line x1="0" y1="540" x2="1920" y2="540" stroke="#4fd1c5" stroke-width="1" />
        <line x1="960" y1="0" x2="960" y2="1080" stroke="#3182ce" stroke-width="1" />
      </g>
    </svg>
    <div class="login-container">
      <el-card class="login-card">
        <h2 class="title">{{ title }}</h2>
        <div class="subtitle">万物互联 · 科技赋能</div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" @submit.native.prevent>
          <el-form-item prop="username" label="账号">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled" label="验证码">
            <div class="code-flex">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="width:100%;margin-bottom:0;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              class="login-btn"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right; margin-top: 10px;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #232a3b 0%, #22304a 100%);
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tech-bg {
  position: absolute;
  top: 0; left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1;
  pointer-events: none;
}
.login-container {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
}
.login-card {
  width: 370px;
  padding: 36px 28px 28px 28px;
  border-radius: 18px;
  background: rgba(30, 41, 59, 0.85);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.25);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(80, 180, 255, 0.08);
}
.title {
  text-align: center;
  margin-bottom: 10px;
  font-size: 2rem;
  font-weight: bold;
  color: #4fd1c5;
  letter-spacing: 2px;
  text-shadow: 0 0 8px #2c5282, 0 0 2px #4fd1c5;
}
.subtitle {
  text-align: center;
  color: #90cdf4;
  font-size: 1.1rem;
  margin-bottom: 28px;
  letter-spacing: 1px;
  text-shadow: 0 0 4px #2c5282;
}
.el-form-item__label {
  color: #b5c6e0;
}
.el-input__wrapper {
  border-radius: 8px;
  background: rgba(44, 62, 80, 0.7);
  box-shadow: 0 1px 4px 0 rgba(80, 180, 255, 0.08);
}
.el-input__inner {
  color: #e6f7ff;
  background: transparent;
}
.login-btn {
  width: 100%;
  border-radius: 8px;
  background: linear-gradient(90deg, #3182ce 0%, #4fd1c5 100%);
  border: none;
  font-weight: bold;
  letter-spacing: 2px;
  box-shadow: 0 2px 8px 0 rgba(80, 180, 255, 0.15);
}
.login-btn:focus {
  outline: none;
  box-shadow: 0 0 0 2px #4fd1c5;
}
.code-flex {
  display: flex;
  align-items: center;
}
.code-flex .el-input {
  flex: 1;
}
.login-code-img {
  height: 38px;
  margin-left: 16px;
  border-radius: 6px;
  box-shadow: 0 1px 4px 0 rgba(80, 180, 255, 0.08);
  background: #fff;
}
.error {
  color: #ff6b81;
  text-align: center;
  margin-top: 10px;
  font-weight: bold;
  letter-spacing: 1px;
}
</style>
